<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>Document</title>
  <style>
    .box{width: 100px;height: 100px;background: red;position: absolute;left:0px;top:0;}
    .line{width: 1px;background: black;position: absolute;left:400px;top:0;height: 400px;}
  </style>
</head>
<body>
  <div class="box"></div>
  <div class="line"></div>
</body>
<script>

  

  const box = document.querySelector(".box");

  let t;
  const target = 300;
  
  box.onclick = function(){
    if(t) return;
    t = setInterval(()=>{
      // 步长为：剩下距离n分之一
      let speed = (target - box.offsetLeft) / 10;
      // 根据正负值，选择向上或向下取整
      speed = speed < 0 ? Math.floor(speed) : Math.ceil(speed);
      
      // 因为缓冲运动，速度越来越慢，最后必然是1像素1像素的往前走，绝对可以正正好好到目标
      if(target === box.offsetLeft){
        clearInterval(t);
      }else{
        box.style.left = box.offsetLeft + speed + "px";
      }
    }, 30);
  }

  // 封装缓冲运动：任意元素任意属性运动到任意目标


  
</script>
</html>